/**
 * 
 */
package com.yidin.logic;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.yidin.entry.AccountDetailEntry;
import com.yidin.entry.LoginInfo;
import com.yidin.entry.MyDataEntry;
import com.yidin.entry.MyDynamicEntry;
import com.yidin.entry.MyFriendListEntry;
import com.yidin.entry.MyPhotoDetailEntry;
import com.yidin.entry.NearByStoreEntry;
import com.yidin.entry.NearbyFriendsEntry;
import com.yidin.entry.NotificationCenterEntry;
import com.yidin.entry.PhotoListEntry;
import com.yidin.entry.RegisterInfo;
import com.yidin.entry.ServiceBack;
import com.yidin.entry.StoreDetailEntry;
import com.yidin.entry.UserLoadCommentListEntry;
import com.yidin.entry.UserLoadGiftListEntry;
import com.yidin.lsc.LscActivity;
import com.yidin.util.FileAdapter;

/**
 * 解释json数据
 * 
 * @author cola
 * 
 */
public class JsonUtil {

	/**
	 * 注册信息
	 * 
	 * @param json
	 * @return
	 */
	public static RegisterInfo getRegisterData(String json) {
		RegisterInfo ri = new RegisterInfo();
		try {
			JSONObject obj = new JSONObject(json);
			ri.e = obj.getString("e");
			String temp = obj.getString("o");
			JSONObject register = new JSONObject(temp);
			ri.AccountId = register.getString("AccountId");
			ri.Password = register.getString("Password");
			ri.Email = register.getString("Email");
			// ri.Membername = register.getString("Membername");
			// ri.IsOpenPosition = register.getString("IsOpenPosition");
			// ri.SearchSize = register.getString("SearchSize");
			// ri.NewsSetting = register.getString("NewsSetting");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			// ri.e = "";
			e.printStackTrace();
		}
		return ri;
	}

	/**
	 * 登陆信息
	 * 
	 * @param json
	 * @return
	 */
	public static LoginInfo getLoginData(String json) {
		LoginInfo ri = new LoginInfo();
		try {
			JSONObject obj = new JSONObject(json);
			ri.e = obj.getString("e");
			String temp = obj.getString("o");
			JSONObject register = new JSONObject(temp);
			ri.AccountId = register.getString("AccountId");
			ri.Password = register.getString("Password");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			if (ri.e == null)
				ri.e = "";
			e.printStackTrace();
		}
		return ri;
	}

	/**
	 * 重置密码
	 * 
	 * @param json
	 * @return
	 */
	public static String getResetPassword(String json) {
		String eo = "";
		try {
			JSONObject obj = new JSONObject(json);
			eo = obj.getString("e");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return eo;
	}

	/**
	 * 我的动态信息
	 * 
	 * @param json
	 * @param l
	 * @return
	 */
	public static List<MyDynamicEntry> getMyDynamicList(String json,
			List<MyDynamicEntry> l) {
		writeFile("getMyDynamicList", json);
		JSONArray numberList = null;
		JSONObject register = null;
		MyDynamicEntry ri = null;
		try {
			JSONObject obj = new JSONObject(json);
			String temp = obj.getString("o");
			numberList = new JSONArray(temp);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			// ri.e = "";
			e.printStackTrace();
		}
		int len = numberList.length();
		for (int n = 0; n < len; n++) {
			try {
				register = numberList.getJSONObject(n);

				ri = null;
				ri = new MyDynamicEntry();
				ri.AccountId = register.getString("AccountId");
				ri.ObjectKind = register.getString("ObjectKind");
				ri.ObjectNo = register.getString("ObjectNo");
				ri.Content = register.getString("Content");
				ri.MemberName = register.getString("MemberName");
				ri.PhotoUrl = register.getString("PhotoUrl");
				ri.Title = register.getString("Title");
				ri.PictureUrl = register.getString("PictureUrl");
				ri.OpTime = register.getString("OpTime");
				l.add(ri);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				l.add(ri);
				e.printStackTrace();
			}
		}
		return l;
	}

	/**
	 * 附近用户列表
	 * 
	 * @param json
	 * @param l
	 * @return
	 */
	public static List<NearbyFriendsEntry> getMyNearbyList(String json,
			List<NearbyFriendsEntry> l) {
		writeFile("getMyNearbyList", json);
		JSONArray numberList = null;
		JSONObject register = null;
		NearbyFriendsEntry ri = null;
		try {
			JSONObject obj = new JSONObject(json);
			String temp = obj.getString("o");
			numberList = new JSONArray(temp);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			// ri.e = "";
			e.printStackTrace();
		}
		int len = numberList.length();
		for (int n = 0; n < len; n++) {
			try {
				register = numberList.getJSONObject(n);

				ri = null;
				ri = new NearbyFriendsEntry();
				ri.AccountId = register.getString("AccountId");
				ri.Introduction = register.getString("Introduction");
				ri.LastLog = register.getString("LastLog");
				ri.PhotoUrl = register.getString("PhotoUrl");
				ri.MemberName = register.getString("MemberName");
				ri.JingDu = register.getString("JingDu");
				ri.WeiDu = register.getString("WeiDu");
				ri.DeviceInfo = register.getString("DeviceInfo");
				ri.Sex = register.getString("Sex");
				ri.IsOnline = register.getString("IsOnline");
				ri.Age = register.getString("Age");
				ri.Birthday = register.getString("Birthday");
				l.add(ri);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				l.add(ri);
				e.printStackTrace();
			}
		}
		return l;
	}

	/**
	 * 主导航之一的附近商家信息
	 * 
	 * @param json
	 * @param l
	 * @return
	 */
	public static List<NearByStoreEntry> getNearByStoreList(String json,
			List<NearByStoreEntry> l) {
		writeFile("getNearByStoreList", json);
		JSONArray numberList = null;
		JSONObject register = null;
		NearByStoreEntry ri = null;
		try {
			JSONObject obj = new JSONObject(json);
			String temp = obj.getString("o");
			numberList = new JSONArray(temp);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			// ri.e = "";
			e.printStackTrace();
		}
		int len = numberList.length();
		for (int n = 0; n < len; n++) {
			try {
				register = numberList.getJSONObject(n);

				ri = null;
				ri = new NearByStoreEntry();
				ri.AccountId = register.getString("AccountId");
				ri.AccountName = register.getString("AccountName");
				ri.JingDu = register.getString("JingDu");
				ri.WeiDu = register.getString("WeiDu");
				ri.PhotoUrl = register.getString("PhotoUrl");
				l.add(ri);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				l.add(ri);
				e.printStackTrace();
			}
		}
		return l;
	}

	/**
	 * 用户主页数据
	 * 
	 * @param json
	 * @param l
	 * @return
	 */
	public static List<UserLoadGiftListEntry> getUserLoadPersonalPage(String json,
			List<UserLoadGiftListEntry> l,MyDataEntry m) {
		writeFile("getUserLoadPersonalPage", json);
		JSONObject register=null;
		JSONArray j = null;
		try {
			JSONArray obj = new JSONArray(json);
			m.e = obj.getJSONObject(0).getString("e");
			m.a = obj.getJSONObject(1).getString("a");
			String temp = "";
			JSONObject k = obj.getJSONObject(0).getJSONObject("o");
			JSONObject f = k.getJSONObject("Info");
			m.GiftCount = f.getString("GiftCount");
			m.Introduction = f.getString("Introduction");
			m.LinkmeCount = f.getString("LinkmeCount");
			m.MemberName = f.getString("MemberName");
			m.MylinkCount = f.getString("MylinkCount");
			m.PhotoUrl = f.getString("PhotoUrl");
			m.Blood = f.getString("Blood");
			m.Age = f.getString("Age");
			temp = k.getString("Items");
			j = new JSONArray(temp);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int len = j.length();
		UserLoadGiftListEntry ri = null;
		for (int n = 0; n < len; n++) {
			try {
				register = j.getJSONObject(n);

				ri = null;
				ri = new UserLoadGiftListEntry();
				ri.Name = register.getString("Name");
				ri.ProductCode = register.getString("ProductCode");
				ri.Url = register.getString("Url");
				l.add(ri);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				l.add(ri);
				e.printStackTrace();
			}
		}
		return l;
	}

	/**
	 * 其它用户详细信息
	 * @param json
	 * @param ri
	 * @return
	 */
	public static AccountDetailEntry getUserLoadProfessionalInfo(String json,
			AccountDetailEntry ri) {
		writeFile("UserLoadProfessionalInfo", json);
		try {
			JSONObject obj = new JSONObject(json);
			ri.e = obj.getString("e");
			String temp = obj.getString("o");
			JSONObject register = new JSONObject(temp);
			ri.AccountId = register.getString("MemberAccount");
			ri.Height = register.getString("Height");
			ri.Hobby = register.getString("Hobby");
			ri.Homepage = register.getString("Homepage");
			ri.IsMarry = register.getString("IsMarry");
			ri.JingDu = register.getString("JingDu");
			ri.PalFor = register.getString("PalFor");

			ri.Profession = register.getString("Profession");
			ri.WeiDu = register.getString("WeiDu");
			ri.Salary = register.getString("Salary");
			ri.School = register.getString("School");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			// ri.e = "";
			e.printStackTrace();
		}
		return ri;
	}

	/**
	 * 注册信息
	 * 
	 * @param json
	 * @return
	 */
	public static StoreDetailEntry getUserLoadShopDetail(String json) {
		writeFile("getUserLoadShopDetail", json);
		StoreDetailEntry ri = new StoreDetailEntry();
		try {
			JSONObject obj = new JSONObject(json);
			ri.e = obj.getString("e");
			String temp = obj.getString("o");
			JSONObject register = new JSONObject(temp);
			ri.AccountId = register.getString("AccountId");
			ri.AccountName = register.getString("AccountName");
			ri.PhotoUrl = register.getString("PhotoUrl");
			 ri.JingDu = register.getString("JingDu");
			 ri.WeiDu = register.getString("WeiDu");
			 ri.Address = register.getString("Address");
			 ri.Content = register.getString("Content");
			 ri.BusinessHours = register.getString("BusinessHours");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			// ri.e = "";
			e.printStackTrace();
		}
		return ri;
	}
	
	/**
	 * 获取用户照片的详细信息
	 * @param json
	 * @param l
	 * @param m
	 */
	public static List<UserLoadGiftListEntry> getUserPhotoDetail(String json,List<UserLoadGiftListEntry> l,MyDataEntry m) {
		writeFile("getUserPhotoDetail", json);
		JSONObject register=null;
		JSONArray j = null;
		try {
			JSONArray obj = new JSONArray(json);
			m.e = obj.getJSONObject(0).getString("e");
			m.a = obj.getJSONObject(1).getString("a");
			String temp = "";
			JSONObject k = obj.getJSONObject(0).getJSONObject("o");
			JSONObject f = k.getJSONObject("Info");
			m.GiftCount = f.getString("GiftCount");
			m.Introduction = f.getString("Introduction");
			m.LinkmeCount = f.getString("LinkmeCount");
			m.MemberName = f.getString("MemberName");
			m.MylinkCount = f.getString("MylinkCount");
			m.PhotoUrl = f.getString("PhotoUrl");
			m.Blood = f.getString("Blood");
			temp = k.getString("Items");
			j = new JSONArray(temp);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		int len = j.length();
		UserLoadGiftListEntry ri = null;
		for (int n = 0; n < len; n++) {
			try {
				register = j.getJSONObject(n);

				ri = null;
				ri = new UserLoadGiftListEntry();
				ri.Name = register.getString("Name");
				ri.ProductCode = register.getString("ProductCode");
				ri.Url = register.getString("Url");
				l.add(ri);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				l.add(ri);
				e.printStackTrace();
			}
		}
		return l;
	}
	
	/**
	 * 更新用户签名
	 * @param json
	 * @param m
	 * @return
	 */
	public static ServiceBack getUserUpdateIntroduction(String json,StoreDetailEntry m){
		writeFile("getUserUpdateIntroduction", json);
		JSONArray obj;
		try {
			obj = new JSONArray(json);
			m.e = obj.getJSONObject(0).getString("e");
			m.a = obj.getJSONObject(1).getString("a");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return m;
	}
	
	/**
	 * 更新用户个人信息
	 * @param json
	 * @param m
	 * @return
	 */
	public static ServiceBack getUserUpdateProfessionalInfo(String json,AccountDetailEntry m){
		writeFile("getUserUpdateIntroduction", json);
		JSONArray obj;
		try {
			obj = new JSONArray(json);
			m.e = obj.getJSONObject(0).getString("e");
			m.a = obj.getJSONObject(1).getString("a");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return m;
	}
	
	/**
	 * 加载相册列表
	 * @param json
	 * @param m
	 * @return
	 */
	public static List<PhotoListEntry> getUserLoadAlbumList(String json,List<PhotoListEntry> l){
		writeFile("getUserLoadAlbumList", json);
		JSONArray numberList = null;
		JSONObject register = null;
		PhotoListEntry ri = null;
		try {
			JSONObject obj = new JSONObject(json);
			String temp = obj.getString("o");
			numberList = new JSONArray(temp);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			// ri.e = "";
			e.printStackTrace();
		}
		int len = numberList.length();
		for (int n = 0; n < len; n++) {
			try {
				register = numberList.getJSONObject(n);

				ri = null;
				ri = new PhotoListEntry();
				ri.Id = register.getString("Id");
				ri.FilePath = register.getString("FilePath");
				ri.FileDesc = register.getString("FileDesc");
				l.add(ri);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				l.add(ri);
				e.printStackTrace();
			}
		}

		return l;
	}
	
	/**
	 * 获取我的好友列表
	 * @param json
	 * @param l
	 * @return
	 */
	public static List<MyFriendListEntry> getUserLoadFriendList(String json,
			List<MyFriendListEntry> l) {
		writeFile("getUserLoadFriendList", json);
		JSONArray numberList = null;
		JSONObject register = null;
		MyFriendListEntry ri = null;
		try {
			JSONObject obj = new JSONObject(json);
			String temp = obj.getString("o");
			numberList = new JSONArray(temp);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			// ri.e = "";
			e.printStackTrace();
		}
		int len = numberList.length();
		for (int n = 0; n < len; n++) {
			try {
				register = numberList.getJSONObject(n);

				ri = null;
				ri = new MyFriendListEntry();
				ri.Id = register.getString("Id");
				ri.AccountId = register.getString("AccountId");
				ri.PhotoUrl = register.getString("PhotoUrl");
				ri.MemberName = register.getString("MemberName");
				ri.Introduction = register.getString("Introduction");
				l.add(ri);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				l.add(ri);
				e.printStackTrace();
			}
		}
		return l;
	}
	
	public static List<UserLoadCommentListEntry> getPhotoCommentList(String json,
			List<UserLoadCommentListEntry> l,MyPhotoDetailEntry ri) {
		writeFile("getPhotoCommentList", json);
		JSONArray numberList = null;
		JSONObject register = null;
		UserLoadCommentListEntry f = null;
		//MyPhotoDetailEntry ri = null;
		try {
			JSONObject obj = new JSONObject(json);
			JSONObject b = obj.getJSONObject("o");
			register = b.getJSONObject("Info");
			
			ri.FilePath = register.getString("FilePath");
			ri.AccountId = register.getString("AccountId");
			ri.PhotoUrl = register.getString("PhotoUrl");
			ri.MemberName = register.getString("MemberName");
			ri.FileDesc = register.getString("FileDesc");
			
			ri.AddressInfo = register.getString("AddressInfo");
			ri.DeviceInfo = register.getString("DeviceInfo");
			ri.ReadCount = register.getString("ReadCount");
			ri.PraiseCount = register.getString("PraiseCount");
			ri.CommentCount = register.getString("CommentCount");
			numberList = b.getJSONArray("Items");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			// ri.e = "";
			e.printStackTrace();
		}
		
		int len = numberList.length();
		for (int n = 0; n < len; n++) {
			try {
				register = numberList.getJSONObject(n);

				f = null;
				f = new UserLoadCommentListEntry();
				f.PhotoUrl = register.getString("PhotoUrl");
				f.MemberName = register.getString("MemberName");
				f.Content = register.getString("Content");
				f.OpTime = register.getString("OpTime");
				f.AccountId = register.getString("AccountId");
				l.add(f);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				//l.add(ri);
				e.printStackTrace();
			}
		}
		return l;
	}
	
	public static List<NotificationCenterEntry> getUerLoadNoticeList(String json,List<NotificationCenterEntry> l){
		writeFile("getUserLoadAlbumList", json);
		JSONArray numberList = null;
		JSONObject register = null;
		NotificationCenterEntry ri = null;
		try {
			JSONObject obj = new JSONObject(json);
			String temp = obj.getString("o");
			numberList = new JSONArray(temp);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			// ri.e = "";
			e.printStackTrace();
		}
		int len = numberList.length();
		for (int n = 0; n < len; n++) {
			try {
				register = numberList.getJSONObject(n);

				ri = null;
				ri = new NotificationCenterEntry();
				ri.ObjectKind = register.getString("ObjectKind");
				ri.ObjectNo = register.getString("ObjectNo");
				ri.Content = register.getString("Content");
				ri.OpTime = register.getString("OpTime");
				ri.PictureUrl = register.getString("PictureUrl");
				ri.Title = register.getString("Title");
				l.add(ri);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				l.add(ri);
				e.printStackTrace();
			}
		}

		return l;
	}
	
	private static void writeFile(String name, String data) {
		
		String path = FileAdapter.getSdCardRootDir()
				+ LscActivity.SERVER_DATA_PATH + name + ".xml";
		if (FileAdapter.isSdCardExist()) {
			FileAdapter.writeTextFile(path, data);// 写缓存文件
		}
	}
}
